package com.youku.adSDK.Utils
{
   import flash.net.navigateToURL;
   import flash.net.URLRequest;
   import flash.external.ExternalInterface;
   
   public class AdOpenWindow extends Object
   {
      
      public function AdOpenWindow() {
         super();
      }
      
      public static var errorInfo:String = null;
      
      public static function navigateOpenURL(param1:String, param2:String = "_blank", param3:String = "") : void {
         var url:String = param1;
         var window:String = param2;
         var features:String = param3;
         errorInfo = null;
         try
         {
            navigateToURL(new URLRequest(url),window);
         }
         catch(e:Error)
         {
            errorInfo = e.message as String;
            trace(e.getStackTrace());
         }
      }
      
      public static function navigateOpen(param1:URLRequest, param2:String = "_blank", param3:String = "") : void {
         var urlrequest:URLRequest = param1;
         var window:String = param2;
         var features:String = param3;
         try
         {
            navigateToURL(urlrequest,window);
         }
         catch(e:Error)
         {
            trace(e.getStackTrace());
         }
      }
      
      public static function open(param1:URLRequest, param2:String = "_blank", param3:String = "") : void {
         if(param2 != "_blank")
         {
            navigateOpenURL(param1.url,param2,param3);
         }
         else
         {
            openURL(param1.url,param2,param3);
         }
      }
      
      public static function openURL(param1:String, param2:String = "_blank", param3:String = "") : void {
         var url:String = param1;
         var window:String = param2;
         var features:String = param3;
         var WINDOW_OPEN_FUNCTION:String = "window.open";
         var myURL:URLRequest = new URLRequest(url);
         var browserName:String = getBrowserName();
         trace("Openwindow.open: " + browserName);
         if(browserName.toLowerCase() == "firefox")
         {
            trace("Openwindow.open: firefox " + WINDOW_OPEN_FUNCTION);
            try
            {
               ExternalInterface.call(WINDOW_OPEN_FUNCTION,url,window,features);
            }
            catch(e:Error)
            {
               navigateOpenURL(url,window,features);
            }
         }
         else if(browserName.toLowerCase() == "ie9" || browserName.toLowerCase() == "ie10")
         {
            trace("whywhy openwindow.ie9 or ie10");
            navigateOpenURL(url,window);
         }
         else if(browserName.toLowerCase() == "ie")
         {
            trace("Openwindow.open: ie " + WINDOW_OPEN_FUNCTION);
            try
            {
               ExternalInterface.call(WINDOW_OPEN_FUNCTION,url,window,features);
            }
            catch(e:Error)
            {
               navigateOpenURL(url,window,features);
            }
         }
         else if(browserName == "Safari")
         {
            navigateOpenURL(url,window);
         }
         else if(browserName == "Opera")
         {
            navigateOpenURL(url,window);
         }
         else if(browserName == "360")
         {
            navigateOpenURL(url,window);
         }
         else
         {
            navigateOpenURL(url,window);
         }
         
         
         
         
         
      }
      
      private static function getBrowserName() : String {
         var browser:String = null;
         var browserAgent:String = "";
         try
         {
            browserAgent = ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");
         }
         catch(e:Error)
         {
            browserAgent = "";
         }
         if(!(browserAgent == null) && browserAgent.indexOf("Firefox") >= 0)
         {
            browser = "Firefox";
         }
         else if(!(browserAgent == null) && browserAgent.indexOf("Safari") >= 0)
         {
            browser = "Safari";
         }
         else if(!(browserAgent == null) && browserAgent.indexOf("MSIE") >= 0)
         {
            browser = "IE";
         }
         else if(!(browserAgent == null) && browserAgent.indexOf("Opera") >= 0)
         {
            browser = "Opera";
         }
         else
         {
            browser = "Undefined";
         }
         
         
         
         if(!(browserAgent == null) && browserAgent.indexOf("MSIE 9") >= 0)
         {
            browser = "IE9";
         }
         if(!(browserAgent == null) && browserAgent.indexOf("MSIE 10") >= 0)
         {
            browser = "IE10";
         }
         if(!(browserAgent == null) && browserAgent.indexOf("360SE") >= 0)
         {
            browser = "360";
         }
         return browser;
      }
   }
}
